<template>
  <div id="app">
    <router-view />
  </div>
</template>
<style>
  @import '~@/styles/common.css';
</style>
<script>
import {getUserInfo} from '@/utils/cookies';
import {platformAppLogin} from '@/api/common';
export default {
  name: 'App',
  data(){
    return {
      websock:null
    }
  },
  created(){
    //this.initWebSocket();
  },
  methods:{
    initWebSocket() { // 初始化weosocket
      console.log(1);
      const wsUrl = 'ws://81.68.96.145:9501/?token=aaaaa';
      this.websock = new WebSocket(wsUrl)
      console.log(this.websock);
      this.websock.onmessage = this.websocketonmessage
      this.websock.onopen = this.websocketonopen
      this.websock.onerror = this.websocketonerror
      this.websock.onclose = this.websocketclose
      this.$store.dispatch('ws/initWS',this.websock);
    },
    websocketonopen() { // 连接建立之后执行send方法发送数据
      console.log('连接成功')
      const actions = {}
      this.websocketsend(JSON.stringify(actions))
    },
    websocketonerror() { // 连接建立失败重连
      console.log('连接失败')
      //this.initWebSocket()
    },
    websocketonmessage(e) { // 数据接收
      console.log('数据推送')
      console.log(e)
      var aaa = eval('(' + e.data + ')')
      let myArr = this.$store.state.user.remoteUnlocking
      if (aaa.sendType == 1) {
        this.$notify.error({
          title: '警告',
          message: aaa.message,
          duration: 0
        })
      }else if(aaa.sendType == 2){
        if(aaa.onoffState == '1'){
          myArr.unshift(aaa)
          console.log('myArr - ', myArr)
          this.$store.commit('SET_REMOTE_UNLOCKING', myArr)
        }else{
          let newArr = myArr.filter(val => {
            return val !== aaa.roomId
          })
          console.log('newArr - ', newArr)
          this.$store.commit('SET_REMOTE_UNLOCKING', newArr)
        }
      }
      // const redata = JSON.parse(e.data)
      // console.log(redata)
    },
    websocketsend(Data) { // 数据发送
      this.websock.send(Data)
    },
    websocketclose(e) { // 关闭
      console.log('断开连接', e)
    }
  }
}
</script>